import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo: {
      username: String,
      nickname: String,
      email: String,
      birth: String,
      phone: String,
      image: String,
      sex: String,
      statement: String,
      point: Number,
      level: String
    },
    pageControl: {
      UIloadDone: false,
      sectionLoadDone: false,
      leftPopupShow: false,
      topPopupShow_search: false,
      topPopupShow_list: false,
      sectionlist: [],
      ad_list: [
        {
          image: require('../static/image/ad/wlop0.jpg'),
          title: 'wlop0',
          desc: ''
        },
        {
          image: require('../static/image/ad/wlop1.jpg'),
          title: 'wlop1',
          desc: ''
        },
        {
          image: require('../static/image/ad/wlop2.jpg'),
          title: 'wlop2',
          desc: ''
        },
        {
          image: require('../static/image/ad/wlop3.jpg'),
          title: 'wlop3',
          desc: ''
        }
      ]
    },
    dataPreview: {
      latest: [{}],
      hotest: [],
      best: []
    },
    meaasgePreview: {}
  },
  getters: {
    topPopouOverlayShow: state => {
      return (
        state.pageControl.topPopupShow_list ||
        state.pageControl.topPopupShow_search
      )
    },
    isAuthenticated: state => {
      if (state.userinfo.username) return true
      else {
        return false
      }
    },
    loadDone: state => {
      return state.pageControl.UIloadDone && state.pageControl.sectionLoadDone
    }
  },
  mutations: {
    openLeftPopup(state) {
      state.pageControl.leftPopupShow = !state.pageControl.leftPopupShow
    },
    openTopPopup_search(state) {
      state.pageControl.topPopupShow_search = !state.pageControl
        .topPopupShow_search
    },
    openTopPopup_list(state) {
      state.pageControl.topPopupShow_list = !state.pageControl.topPopupShow_list
    },
    closeTopPopup(state) {
      state.pageControl.topPopupShow_list = false
      state.pageControl.topPopupShow_search = false
    },
    getCookie(state, name) {
      var r = document.cookie.match('\\b' + name + '=([^;]*)\\b')
      console.log(name)
      console.log(r)
      return r ? r[1] : undefined
    },
    login(state, userinfo) {
      state.userinfo.username = userinfo.username
      state.userinfo.nickname = userinfo.nickname
      state.userinfo.email = userinfo.email
      state.userinfo.birth = userinfo.birth
      state.userinfo.image = userinfo.image
      state.userinfo.sex = userinfo.sex
      state.userinfo.statement = userinfo.statement
      state.userinfo.point = userinfo.point
      state.userinfo.level = userinfo.level
      state.userinfo.phone = userinfo.phone
    }
  },
  actions: {}
})
